// pages/releasePartTime/releasePartTime.js
var app = getApp()
const QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
const wxMap = new QQMapWX({
  key: 'AB3BZ-QEI6J-7SAFK-FV2J4-YEAR7-C7FYZ' // 必填
});

Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseUrl: '',
    array: ['酒店', '活动', '群演', '安保', '礼仪', '发单', '话务', '工厂', '充场', '家教', '其他'],
    index: '',
    jiesuan: ['日结', '月结'],
    jsindex: '',
    mapShow: false,
    fontcolor: '#000',
    colors: [],
    startDate: '',
    startdetailDate: '',
    endDate: '',
    enddetailDate: '',
    lastDate: '',
    lastdetailDate: '',
    region: ["山东省", "青岛市", "黄岛区"],
    customItem: '全部',
    isclick:true,

    type: 1, //兼职
    job_type: 1, //兼职类型*
    // title: '', //兼职名称
    color: 0, //颜色
    latitude: 0,
    longitude: 0,
    ad_type: '',  //行政区级别*
    ad_code: '',  //行政区区划码*
    // people_num: '',  //兼职人数
    // gender_needed: 0, //姓别要求
    sign_end: 0, //报名截止时间
    work_start: 0, //工作开始时间
    worl_end: 0, //工作结束时间
    // salary: '', //薪资水平
    // pay_typed: '', //结算类型
    content: '', //详情
    // contact_name: '',//联系人
    // contact_phone: '',//联系人电话
  },
  validate: function () {
    if (this.data.job_type == 0) {
      wx.showToast({
        title: '选择兼职类型',
      })
      return false
    }
    if (this.data.ad_code == '') {
      wx.showToast({
        title: '选择具体位置',
      })
      return false
    }

    if (this.data.startDate == '') {
      wx.showToast({
        title: '选择开始日期',
      })
      return false
    }
    // if (this.data.endDate == ''){
    //   wx.showToast({
    //     title: '选择结束日期',
    //   })
    //   return false
    // }
    if (this.data.startdetailDate == '') {
      wx.showToast({
        title: '选择上班时间',
      })
      return false
    }
    if (this.data.content == '') {
      wx.showToast({
        title: '填写兼职详情',
      })
      return false
    }


    return true
  },
  submitinfo: function () {
    if (this.data.isclick) {
      this.setData({
        isclick: false
      })
      setTimeout(() => {
        this.setData({
          isclick: true
        })
      }, 5000);
    }
    if(this.validate()){
      wx.showLoading({
        title: '请稍等',
      })
      var accesstoken = wx.getStorageSync('accesstoken')
      if (this.data.color != 0) {
        wx.request({
          url: 'https://api.tianlala.com.cn/part-times', // 仅为示例
          data: {
            job_type: this.data.job_type, //兼职类型*
            // title: this.data.title, //兼职名称
            content: this.data.content, //详情
            // contact_name: this.data.contact_name, //联系人
            // contact_phone: this.data.contact_phone, //联系人电话
            // people_num: this.data.people_num,   //兼职人数
            // sign_end: this.data.sign_end,  //报名截止时间
            sign_end: this.data.work_start,  //报名截止时间
            start_at: this.data.work_start,  //兼职开始时间(时间戳)
            end_at: this.data.worl_end,  //兼职结束时间(时间戳)
            // work_start: this.data.startdetailDate,  //工作开始时间
            // work_end: this.data.enddetailDate,  //工作结束时间
            latitude: String(this.data.latitude),
            longitude: String(this.data.longitude),
            ad_code: this.data.ad_code,   //行政区区划码*
            // ad_type: this.data.ad_type,   //行政区级别*
            // salary: this.data.salary,  //薪资水平
            // gender_needed: this.data.gender_needed,  //姓别要求
            // pay_type: this.data.pay_typed,  //结算类型
            good_id: this.data.color,  //颜色

          },
          method: 'POST',
          header: {
            'content-type': 'application/json', // 默认值
            'Authorization': accesstoken
          },
          success(res) {
            console.log(res)
            wx.hideLoading();
            if (res.statusCode == 201) {
              if (res.data.pay_needed == 1) {
                wx.request({
                  url: 'https://api.tianlala.com.cn/order',
                  data: {
                    out_trade_no: res.data.order_id
                  },
                  method: 'POST',
                  header: {
                    // 'content-type': 'application/json', // 默认值
                    'Authorization': accesstoken
                  },
                  success(res) {
                    console.log(res, "支付")
                    wx.requestPayment({
                      timeStamp: res.data.result.timeStamp,
                      nonceStr: res.data.result.nonceStr,
                      package: res.data.result.package,
                      signType: res.data.result.signType,
                      paySign: res.data.result.paySign,
                      success(res) {
                        console.log(res, "支付成功");
                        wx.showToast({
                          title: '提交成功',
                        })
                        setTimeout(() => {
                          wx.switchTab({
                            url: '/pages/index/index',
                          })
                        }, 2000)
                      },
                      fail(res) {
                        console.log(res, "支付失败");
                      }
                    })
                  }
                })
              }else{
                wx.showToast({
                  title: '提交成功',
                })
                setTimeout(() => {
                  wx.switchTab({
                    url: '/pages/index/index',
                  })
                }, 2000)
              }

            } else {
              wx.showToast({
                title: res.data[0].message,
                icon: "none",
                duration: 1500
              })
            }
          }
        })
      } else {
        wx.request({
          url: 'https://api.tianlala.com.cn/part-times', // 仅为示例
          data: {
            job_type: this.data.job_type, //兼职类型*
            // title: this.data.title, //兼职名称
            content: this.data.content, //详情
            // contact_name: this.data.contact_name, //联系人
            // contact_phone: this.data.contact_phone, //联系人电话
            // people_num: this.data.people_num,   //兼职人数
            // sign_end: this.data.sign_end,  //报名截止时间
            sign_end: this.data.work_start,  //报名截止时间
            start_at: this.data.work_start,  //兼职开始时间(时间戳)
            end_at: this.data.worl_end,  //兼职结束时间(时间戳)
            // work_start: this.data.startdetailDate,  //工作开始时间
            // work_end: this.data.enddetailDate,  //工作结束时间
            latitude: String(this.data.latitude),
            longitude: String(this.data.longitude),
            ad_code: this.data.ad_code,   //行政区区划码*
            // ad_type: this.data.ad_type,   //行政区级别*
            // salary: this.data.salary,  //薪资水平
            // gender_needed: this.data.gender_needed,  //姓别要求
            // pay_type: this.data.pay_typed,  //结算类型
            // good_id: this.data.color,  //颜色

          },
          method: 'POST',
          header: {
            'content-type': 'application/json', // 默认值
            'Authorization': accesstoken
          },
          success(res) {
            console.log(res)
            wx.hideLoading();
            if (res.statusCode == 201) {
              if (res.data.pay_needed == 1) {
                wx.request({
                  url: 'https://api.tianlala.com.cn/order',
                  data: {
                    out_trade_no: res.data.order_id
                  },
                  method: 'POST',
                  header: {
                    // 'content-type': 'application/json', // 默认值
                    'Authorization': accesstoken
                  },
                  success(res) {
                    console.log(res, "支付")
                    wx.requestPayment({
                      timeStamp: res.data.result.timeStamp,
                      nonceStr: res.data.result.nonceStr,
                      package: res.data.result.package,
                      signType: res.data.result.signType,
                      paySign: res.data.result.paySign,
                      success(res) {
                        console.log(res, "支付成功");
                        wx.showToast({
                          title: '提交成功',
                        })
                        setTimeout(() => {
                          wx.switchTab({
                            url: '/pages/index/index',
                          })
                        }, 2000)
                      },
                      fail(res) {
                        console.log(res, "支付失败");
                      }
                    })
                  }
                })
              } else {
                wx.showToast({
                  title: '提交成功',
                })
                setTimeout(() => {
                  wx.switchTab({
                    url: '/pages/index/index',
                  })
                }, 2000)
              }
              
            } else {
              wx.showToast({
                title: res.data[0].message,
                icon: "none",
                duration: 1500
              })

            }
          }
        })
      }
    }


  },
  // 兼职类型
  bindduolieChange: function (e) {
    // console.log(e.detail.value)
    this.setData({
      index: e.detail.value,
      job_type: this.data.array[e.detail.value].id
    })
  },

  //兼职名称
  jianzhiname: function (e) {
    this.setData({
      title: e.detail.value
    })
  },
  //选择颜色
  fontColorChange: function (e) {
    console.log(e.currentTarget.dataset.color)
    this.setData({
      fontcolor: e.currentTarget.dataset.color,
      color: e.currentTarget.dataset.colorid
    })
  },


  regionchange(e) {
    // console.log(e.type)
  },
  markertap(e) {
    console.log(e.markerId)
  },
  controltap(e) {
    console.log(e.controlId)
  },
  //选择区
  bindRegionChange: function (e) {
    this.setData({
      region: e.detail.value
    })
  },
  //选择具体位置
  selectdetailadress: function () {
    const that = this;
    wx.chooseLocation({
      success: (res) => {
        console.log(res)
        wxMap.reverseGeocoder({
          location: {
            latitude: res.latitude,
            longitude: res.longitude,
          },
          success: function (res) {
            console.log(res.result.ad_info, '逆地址解析');
            that.setData({
              ad_code: res.result.ad_info.adcode
            })
          },
        });

        this.setData({
          latitude: res.latitude,
          longitude: res.longitude,
          markers: [{
            iconPath: '/img/fabu/position.png',
            id: 0,
            latitude: res.latitude,
            longitude: res.longitude,
            width: 30,
            height: 30
          }],
          controls: [{
            id: 1,
            iconPath: '/img/fabu/position.png',
            position: {
              left: 0,
              top: 0,
              width: 30,
              height: 30
            },
            clickable: true
          }],
          mapShow: true
        })
      }
    })
  },
  //兼职人数
  jianzhinum: function (e) {
    this.setData({
      people_num: e.detail.value
    })
  },
  //性别要求
  xingbieyaoqiu: function (e) {
    this.setData({
      gender_needed: e.detail.value
    })
  },
  //截止时间
  bindlastDateChange: function (e) {
    this.setData({
      lastDate: e.detail.value
    })
    var date = this.data.lastDate + ' 00:00:00';
    // console.log(date)
    // var date = '2015-03-05 17:59';
    date = date.substring(0, 15);
    date = date.replace(/-/g, '/');
    var timestamp = new Date(date).getTime();
    // console.log(timestamp)

    this.setData({
      sign_end: String(timestamp),
     
    })
  },
  bindlastdetailDate: function (e) {
    this.setData({
      lastdetailDate: e.detail.value
    })
    var date = this.data.lastDate + ' ' + this.data.lastdetailDate;
    // console.log(date)
    // var date = '2015-03-05 17:59';
    date = date.substring(0, 15);
    date = date.replace(/-/g, '/');
    var timestamp = new Date(date).getTime();
    // console.log(timestamp)
    this.setData({
      sign_end: String(timestamp)
    })
  },
  //开始时间
  bindStartDateChange: function (e) {
    this.setData({
      startDate: e.detail.value
    })
    var date = this.data.startDate + ' 00:00:00';
    // console.log(date)
    // var date = '2015-03-05 17:59';
    date = date.substring(0, 15);
    date = date.replace(/-/g, '/');
    var timestamp = new Date(date).getTime() / 1000;
    var signEnd = timestamp - 6*60*60;
    var worlEnd = timestamp + 3*24*60*60;
    // console.log(timestamp)
    this.setData({
      sign_end: String(signEnd),
      work_start: String(timestamp),
      worl_end: String(worlEnd)
    })
  },
  bindstartdetailDate: function (e) {
    this.setData({
      startdetailDate: e.detail.value
    })
    var date = this.data.startDate + ' ' + this.data.startdetailDate;
    // console.log(date)
    // var date = '2015-03-05 17:59';
    date = date.substring(0, 15);
    date = date.replace(/-/g, '/');
    var timestamp = new Date(date).getTime() / 1000;
    // console.log(timestamp)
    this.setData({
      work_start: String(timestamp)
    })
  },
  //结束时间
  bindEndDateChange: function (e) {
    this.setData({
      endDate: e.detail.value
    })
    var date = this.data.endDate + ' 00:00:00';
    console.log(date)
    // var date = '2015-03-05 17:59';
    date = date.substring(0, 15);
    date = date.replace(/-/g, '/');
    var timestamp = new Date(date).getTime() / 1000;
    console.log(timestamp)
    this.setData({
      worl_end: String(timestamp)
    })
  },
  bindenddetailDate: function (e) {
    this.setData({
      enddetailDate: e.detail.value
    })
    var date = this.data.endDate + ' ' + this.data.enddetailDate;
    // console.log(date)
    // var date = '2015-03-05 17:59';
    date = date.substring(0, 15);
    date = date.replace(/-/g, '/');
    var timestamp = new Date(date).getTime();
    // console.log(timestamp)
    this.setData({
      worl_end: String(timestamp)
    })
  },
  //薪资
  jianzhimoeny: function (e) {
    var value = e.detail.value.replace(/[^\d]/g, '')
    console.log(value)
    this.setData({
      salary: value
    })
  },

  //结算类型
  bindjiesuanChange: function (e) {
    // console.log(e.detail.value)
    var paytyped = 1
    if (e.detail.value == 0) {
      paytyped = 1
    } else if (e.detail.value == 1) {
      paytyped = 2
    }
    // console.log(paytyped)
    this.setData({
      jsindex: e.detail.value,
      pay_typed: paytyped
    })
  },
  textdetail: function (e) {
    this.setData({
      content: e.detail.value
    })
  },
  lianxi: function (e) {
    this.setData({
      contact_name: e.detail.value
    })
  },
  phone: function (e) {
    this.setData({
      contact_phone: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      baseUrl: app.baseUrl
    })
    var accesstoken = wx.getStorageSync('accesstoken')
    if (!this.data.colors[0]) {
      wx.request({
        url: 'https://api.tianlala.com.cn/colors', // 仅为示例
        data: {
        },
        method: 'GET',
        header: {
          'content-type': 'application/json', // 默认值
          'Authorization': accesstoken
        },
        success: (res) => {
          console.log(res, '颜色')
          this.setData({
            colors: res.data
          })
        }
      })
    }
    if (true) {
      wx.request({
        url: 'https://api.tianlala.com.cn/job-types/job', // 仅为示例
        data: {
        },
        method: 'GET',
        header: {
          'content-type': 'application/json', // 默认值
          'Authorization': accesstoken
        },
        success: (res) => {
          console.log(res, '类型')
          this.setData({
            array: res.data
          })
        }
      })
    }
    wx.getLocation({
      type: 'wgs84',
      success: (res) => {
        this.setData({
          latitude: res.latitude,
          longitude: res.longitude,
          markers: [{
            iconPath: '/img/fabu/position.png',
            id: 0,
            latitude: res.latitude,
            longitude: res.longitude,
            width: 30,
            height: 30
          }],
          controls: [{
            id: 1,
            iconPath: '/img/fabu/position.png',
            position: {
              left: 0,
              top: 0,
              width: 30,
              height: 30
            },
            clickable: true
          }]
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})